<?php
	//View.php
	
	class View
	{
		protected static $template;
		protected static $mainKeys = array(); 

		/*
		*Set main html template
		*
		*
		*@param template: Path to main template of site
		*return Exception
		*/
		public static function setTemplate($template)
		{
			if(file_get_contents($template))
			{
				self::$template = file_get_contents($template);
				return true;
			}
			else
			{
				throw new Exception('Html file not exists!');
			}
		}
		
		/*
		*Main function - Parse and create html template
		*
		*
		*@param html: String with html template
		*@param array: Array with keys
		*return string: Return string
		*/
		public static function create($html,$array)
		{
			if($html = file_get_contents($html))
			{
				$html = self::replaceKeys($html,$array);
				return $html;
			}
			else
			{
				throw new Exception('Html file not exists!');
			}
		}
		/*
		*Get midle parth of string between start and end value
		*
		*
		*@param html: String with html template
		*@param start: Start string
		*@param end: End string
		*return string: Return string between start snd end
		*/
		private static function getMiddle($html,$start,$end)
		{
			$startPos = strpos($html,$start) + strlen($start);
			$endPos = strpos($html,$end);
			$middleLenght = $endPos - $startPos;
			$middleHtml = substr($html,$startPos,$middleLenght);
			return $middleHtml;
		}
		
		/*
		*Delete string between start and end value
		*
		*
		*@param html: String with html template
		*@param start: Start string
		*@param end: End string
		*return string: Return string without data between start and end
		*/
		private static function deleteMiddle($html,$start,$end)
		{
			$middle = self::getMiddle($html,$start,$end);
			return str_replace($start.$middle.$end,'',$html);	
		}
		
		/*
		*Parse and recompilate "if-else" construction in string
		*
		*
		*@param html: String with html template
		*@param array: Array with keys
		*return string: Return recompilating string with "if-else" result
		*/
		private static function replaceIf($html,$array)
		{
			while(strpos($html,"<!--if{") !== false && 
				   strpos($html,"<!--if{") < strpos($html,"<!--foreach_") OR 
				  (strpos($html,"<!--foreach_") === false && 
				   strpos($html,"<!--if{") !== false))
			{


				$result = false;
				$middle = self::getMiddle($html,'<!--if{','}-->');
				if(strpos($middle,'isset(') !== false)
				{
					
					$var = self::getMiddle($html,'<!--if{isset(',')}-->');
					if(isset($array[$var]))
					{
						$result = true;
					}
				}
				elseif(strpos($middle,'!empty(') !== false)
				{
					$var = self::getMiddle($html,'<!--if{!empty(',')}-->');
					if(!empty($array[$var]))
					{
						$result = true;
					}
				}
				elseif(strpos($middle,'constant(') !== false)
				{
					$var = self::getMiddle($middle,'constant(',')');
					$param = explode("=",$var);
					if(defined($param[0]))
					{
						if(constant($param[0]) == $param[1])
						{
							$result = true;
							break;
						}
					}
				}
				elseif(strpos($middle,'='))
				{
					$param = explode("=",$middle);
					if(strpos($param[1],'||'))
					{
						$or = explode("||",$param[1]);
						foreach($or as $key=>$value)
						{

							if($array[$param[0]] == $value)
							{
								$result = true;
								break;
							}
						}
					}
					elseif(isset($array[$param[0]]) && $array[$param[0]] == $param[1])
					{
						$result = true;
					}
				}
				elseif(strpos($middle,'>'))
				{
					$param = explode(">",$middle);
					if($array[$param[0]] > $param[1])
					{
						$result = true;
					}
				}
				elseif(strpos($middle,'<'))
				{
					$param = explode("<",$middle);
					if($array[$param[0]] < $param[1])
					{
						$result = true;
					}
				}
				
				if($result)
				{
					$html = str_replace("<!--if{".$middle."}-->",'',$html);
					if(strpos($html,"<!--else{".$middle."}-->"))
					{
						$html = self::deleteMiddle($html,"<!--else{".$middle."}-->","<!--endif{".$middle."}-->");
					}
					else
					{
						//$html = str_replace("<!--endif{".$middle."}-->",'',$html);
					}	
				
				}
				else
				{
					if(strpos($html,"<!--else{".$middle."}-->"))
					{
						$html = self::deleteMiddle($html,"<!--if{".$middle."}-->","<!--else{".$middle."}-->"); 
						$html = str_replace("<!--endif{".$middle."}-->",'',$html);
					}
					else
					{
						$middleDel = self::getMiddle($html,"<!--if{".$middle."}-->","<!--endif{".$middle."}-->");
						$html = str_replace("<!--if{".$middle."}-->".$middleDel."<!--endif{".$middle."}-->",'',$html);
					}	
				}		
			}
			return $html;	
		}	
		
		/*
		*Parse and replace keys in template
		*
		*
		*@param html: String with html template
		*@param array: Array with keys
		*return string: Return recompilating string with "if-else" result
		*/
		private static function replaceKeys($html,$array)
		{
			if(!empty($array))
			{
				$html = self::replaceIf($html,$array);
				foreach($array as $key=>$value)
				{
					if(is_array($value))
					{
						$startValue = "<!--foreach_$key-->";
						$endValue = "<!--endforeach_$key-->";
						$template = self::getMiddle($html,$startValue,$endValue);	
						$rows = '';
							foreach($value as $k=>$v)
							{
								if(is_array($v))
								{
									$rows .= self::replaceKeys($template,$v);
								}	
							}
						$html = str_replace($startValue.$template.$endValue,$rows,$html);
						$html = self::replaceIf($html,$array);
					}
					else
					{
					
						$html = str_replace("%$key%",$value,$html);	
					}	
				}
			}
			$html = self::replaceIf($html,$array);
			return $html;
		}	

		/*
		*Create error template
		*
		*
		*@param message: Error message
		*return string: Return html template with error message
		*/
		public static function error($message)
		{
			$errorHtml = file_get_contents(TEMPLATES_DIR.'error.html');
			$errorHtml = str_replace('%error%',$message,$errorHtml);
			return $errorHtml;
		}

		/*
		*Create error template
		*
		*
		*@param message: Error message
		*return string: Return html template with error message
		*/
		public static function errorWindow($message)
		{
			$errorHtml = file_get_contents(TEMPLATES_DIR.'appointment/errorWindow.html');
			$errorHtml = str_replace('%error%',$message,$errorHtml);
			return $errorHtml;
		}	
		/*
		*Create message template
		*
		*
		*@param message: Message
		*return string: Return html template with message
		*/
		public static function message($message)
		{
			$successHtml = file_get_contents(TEMPLATES_DIR.'message.html');
			$successHtml = str_replace('%message%',$message,$successHtml);
			return $successHtml;
		}

		/*
		*Assign key in main template
		*
		*
		*@param mainKey: Title of key in main template
		*@param html: string with html template
		*return none
		*/
		public static function assign($mainKey,$html)
		{
			if(isset(self::$mainKeys[$mainKey]))
			{
				self::$mainKeys[$mainKey] .= $html;
			}
			else
			{
				self::$mainKeys[$mainKey] = $html;
			}
		}	
		
		/*
		*Assign keys in main template
		*
		*
		*@param arrayKeys: Array[title of main tag] = 'value'
		*return none
		*/
		public static function assignALL($arrayKeys)
		{
			self::$mainKeys = $arrayKeys;
		}
		
		/*
		*Delete all data
		*/
		public static function deleteAll()
		{
			self::$mainKeys = '';
		}
		/*
		*Print main template
		*
		*
		*return: echo template;
		*/
		public static function display()
		{
			self::$template = self::replaceKeys(self::$template,self::$mainKeys);			
			print self::$template;	
		}
		
	}
